package com.imooc.news_manager_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.news_manager_project.common.Result;
import com.imooc.news_manager_project.common.enums.RoleEnum;
import com.imooc.news_manager_project.config.BimConfig;
import com.imooc.news_manager_project.domain.User;
import com.imooc.news_manager_project.dto.LoginDTO;
import com.imooc.news_manager_project.dto.UserDTO;
import com.imooc.news_manager_project.mapper.UserMapper;
import com.imooc.news_manager_project.query.UserQuery;
import com.imooc.news_manager_project.service.IUserService;
import com.imooc.news_manager_project.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author coder
 * @description 针对表【t_user(用户数据表)】的数据库操作Service实现
 * @createDate 2025-07-03 11:36:16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private BimConfig bimConfig;


    @Override
    public Result save(UserDTO userDTO) {
        String role = userDTO.getRole();

        if (!RoleEnum.isContains(role)) {
            return new Result(400, "角色错误");
        }

        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setId(null);
        user.setCreateTime(new Date());
        user.setCreateUser("ceshi");
        save(user);
        if (user.getId() != null) {
            return new Result(200, "保存成功", user.getId());
        }
        return new Result(400, "保存失败");
    }


    public Result listParams(UserQuery userQuery) {
        QueryWrapper<User> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.like(userQuery.getName() != null, "name", userQuery.getName());
        QueryWrapper.like(userQuery.getMobile() != null, "mobile", userQuery.getMobile());
        QueryWrapper.like(userQuery.getRole() != null, "role", userQuery.getRole());
        QueryWrapper.like(userQuery.getId() != null, "id", userQuery.getId());
        QueryWrapper.eq("delete_flag", 0);
        List<User> userList = list(QueryWrapper);
        return new Result(200, "查询成功", userList);
    }

    public Result lists() {
        return new Result(200, "查询成功", list());
    }

    public Result update(UserDTO userDTO) {
        User userOld = getById(userDTO.getId());
        if (userOld == null) {
            return new Result(400, "用户不存在");
        }
        BeanUtils.copyProperties(userDTO, userOld);
        boolean updateRes = updateById(userOld);
        if (updateRes) {
            return new Result(200, "更新成功");
        }
        return new Result(400, "更新失败");
    }

    public Result remove(Integer id) {
        User user = getById(id);
        if (user == null) {
            return new Result(400, "用户不存在");
        }
        user.setDeleteFlag(1);
        user.setUpdateUser("ceshi");
        user.setUpdateTime(new Date());
        boolean res = updateById(user);
        if (res) {
            return new Result(200, "删除成功");
        }
        return new Result(400, "删除失败");

    }

    public Result login(LoginDTO loginDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginDTO.getName());
        queryWrapper.eq("password", loginDTO.getPassword());
        queryWrapper.eq("delete_flag", 0);
        User user = getOne(queryWrapper, false);
        if (user == null) {
            return new Result(101, "用户名或密码错误");
        }

        String token = UUID.randomUUID().toString();
        boolean res = redisUtil.set(token, user, bimConfig.getExpirationTime());
        if (res) {
            return new Result(200, "登录成功", token);
        }

        return new Result(101, "登录失败");
    }

    public Result logout(String token) {
        boolean res = redisUtil.remove(token);
        if (res) {
            return new Result(200, "退出成功");
        }
        return new Result(101, "退出失败");
    }
}






















